home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12178 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Settle a bet please
  5. Date: 29 Mar 1996 10:06:53 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jh8rtINNosd@mayne.ugrad.cs.ubc.ca>
  8. References: <4jfopb$o9n@news1.sympatico.ca>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <4jfopb$o9n@news1.sympatico.ca>,
  12. Gisele Swinson  <gisele.swinson@sympatico.ca> wrote:
  13. >In C language, how do they calculate the length of an array.
  14. >
  15. >example
  16. >
  17. >To declare a string "My Name"
  18. >
  19. >is it char name[7] = "My Name"
  20. >or
  21. >is it chat Name[8] = "My Name"
  22. >
  23. >There is a battle in my class whether to include the NULL in the
  24. >array size. 
  25.  
  26. It's a null character, not the NULL pointer.
  27.  
  28. >I would appreciate any input you might have.
  29.  
  30. The proper way is to leave the size out unless you will be writing some other
  31. string into the same field.
  32.  
  33. char name[] = "My Name";
  34.  
  35.  
  36. If you give a specific size, you must include storage for the terminating zero,
  37. IF YOU EXPECT ``name'' to be a STRING.
  38.  
  39. If you don't care about name being a string, you can declare the size as 7. The
  40. standard allows that leeway, I think (I would have to check!). In that case,
  41. you just have an array of the characters { 'M', 'y', 'n', 'a', 'm', 'e' }. It
  42. is not a string, and should not be used as an argument to string manipulating
  43. functions.
  44.  
  45. So nobody has won the bet, because you have not decided whether you are sizing
  46. a string or a plain array.
  47. -- 
  48.  
  49.